package com.rk.kbk.controller.core;

import java.io.*;
import java.net.InetSocketAddress;
import java.net.Socket;

/**
 * Created by mifan on 2017/5/15.
 */
public class ClientDemo {
    public static String clientip = "60.191.49.59";
    public static int port = 8787;

    public static void main(String[] args) {
        int length = 0;
        byte[] sendBytes = null;
        Socket socket = null;
        DataOutputStream dos = null;
        FileInputStream fis = null;
        try {
            try {
                socket = new Socket();
                socket.connect(new InetSocketAddress(clientip, port),30 * 1000);
                dos = new DataOutputStream(socket.getOutputStream());
                File file = new File("C:\\Users\\Administrator\\Documents\\Tencent Files\\646268716\\FileRecv\\9.jpg");
                fis = new FileInputStream(file);
                sendBytes = new byte[1024*4];
                while ((length = fis.read(sendBytes, 0, sendBytes.length)) > 0) {
                    dos.write(sendBytes, 0, length);
                    dos.flush();
                }
                socket.shutdownOutput();
                //3、获取输入流，并读取服务器端的响应信息
                InputStream is = socket.getInputStream();
                BufferedReader br = new BufferedReader(new InputStreamReader(is,"utf-8"));
                String info = null;
                //System.out.println("我是客户端，服务器说：122222"+br.readLine());
                while((info=br.readLine())!=null){
                    System.out.println("我是客户端，服务器说：23"+info);
                }
            } finally {
                if (dos != null)
                    dos.close();
                if (fis != null)
                    fis.close();
                if (socket != null)
                    socket.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
